package com.fenghuajueli.module_find_the_difference.fragment;

import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.SizeUtils;
import com.fenghuajueli.module_find_the_difference.SPHelper;
import com.fenghuajueli.module_find_the_difference.R;
import com.fenghuajueli.module_find_the_difference.adapter.GuankaAdapter;
import com.fenghuajueli.module_find_the_difference.databinding.FragmentZhaochaBinding;
import com.fenghuajueli.module_find_the_difference.entity.Guanka;
import com.fenghuajueli.module_route.HomeModuleRoute;

import java.util.ArrayList;
import java.util.List;

public class ZhaochaFragment extends Fragment {
    private FragmentZhaochaBinding binding;
    private int index;
    private List<Guanka> guankaList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentZhaochaBinding.inflate(getLayoutInflater());
        initData();
        return binding.getRoot();
    }

    private void initData() {
        index = SPHelper.getInstance(getActivity()).getIndex();
        guankaList.clear();
        for (int i = 0; i < 26; i++) {
            Guanka guanka = new Guanka();
            guanka.setUnLocked(false);
            guanka.setGuankaId(i + 1);
            guanka.setTitle("关卡" + (i + 1));
            guankaList.add(guanka);
        }

        for (int i = 0; i < index+1; i++) {
            if (i != 26) {
                guankaList.get(i).setUnLocked(true);
            }
        }

        GuankaAdapter guankaAdapter = new GuankaAdapter(getActivity(), R.layout.item_card, guankaList);
        binding.rvZhaocha.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        binding.rvZhaocha.setAdapter(guankaAdapter);
        if (binding.rvZhaocha.getItemDecorationCount() == 0) {
            binding.rvZhaocha.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    outRect.top = SizeUtils.dp2px(15);
                    if (parent.getChildLayoutPosition(view) % 2 == 1) {
                        outRect.left = SizeUtils.dp2px(5);
                    }
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}